﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using FTBS.Models.BLO;

namespace FTBS.Controllers
{
    public class AccessPermissionController : Controller
    {
        private UserBLO _userBlo = new UserBLO();

        /// <summary>
        /// check login of account and get role
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>roleName</returns>
        /// <author>HungPH</author>
        /// <date>3/4/2014</date>
        public String CheckLogin(string username, string password)
        {
            String roleName = "";
            string pass = _userBlo.GenerateMD5(password);
            roleName = _userBlo.GetRoleNameFromLogin(username, pass);
            if (roleName != "")
            {
                if (!_userBlo.GetStatusOfUser(username))
                {
                    roleName = "Deactive";
                }
                else if (_userBlo.GetStatusOfUser(username))
                {
                this.Session["RoleName"] = roleName;
                this.Session["Account"] = _userBlo.LoadUserByUserName(username).FullName;
                this.Session["Number"] = _userBlo.LoadUserByUserName(username).PhoneNumber;
                this.Session["Email"] = _userBlo.LoadUserByUserName(username).Email;
                this.Session["UserName"] = username;
                }
                
            }
            
            return roleName;
        }

        /// <summary>
        /// Log out
        /// </summary>
        /// <returns>true/false</returns>
        /// <author>hungph</author>
        /// <date>3/6/2014</date>
        public String LogOut()
        {
            this.Session["RoleName"] = null;
            this.Session["Account"] = null;
            this.Session["UserName"] = null;
            this.Session["Number"] = null;
            this.Session["Email"] = null;
            return "Success";
        }

    }
}
